﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MediaLibrary.Controls;
using AMPLibrary.Collections;
using AMPLibrary.Media;

namespace MediaLibrary
{
    /// <summary>
    /// Interaction logic for SearchPage.xaml
    /// </summary>
    public partial class SearchPage : Page
    {
        Collection<MediaFile> musicArtistResultsCollection = new Collection<MediaFile>();

        Collection<MediaFile> musicAlbumResultsCollection = new Collection<MediaFile>();

        Collection<MediaFile> musicSongResultsCollection = new Collection<MediaFile>();

        Collection<MediaFile> musicGenreResultsCollection = new Collection<MediaFile>();

        Collection<MediaFile> videoResultsCollection = new Collection<MediaFile>();

        Collection<Playlist> playlistResultsCollection = new Collection<Playlist>();

        MusicCollection musicCollection = MusicCollection.GetMusicCollection();

        VideoCollection videoCollection = VideoCollection.GetVideoCollection();

        PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();

        /// <summary>
        /// 
        /// </summary>
        public SearchPage()
        {
            InitializeComponent();

            detailedResultsListView.ItemTemplateSelector = new SearchResultsDataTemplateSelector();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="searchString"></param>
        public void Search(string searchString)
        {
            searchString = searchString.ToLower().Trim();

            detailedResultsListView.ItemsSource = null;

            resultsListView.Items.Clear();

            // music results
            musicArtistResultsCollection = new Collection<MediaFile>((from c in musicCollection where c.Tag.AlbumArtist.ToLower().Contains(searchString) select c).Distinct());

            if (musicArtistResultsCollection.Count > 0)
                resultsListView.Items.Add("Artist matches: " + (from r in musicArtistResultsCollection select r.Tag.AlbumArtist).Distinct().Count().ToString());


            // album results
            musicAlbumResultsCollection = new Collection<MediaFile>((from c in musicCollection where c.Tag.Album.ToLower().Contains(searchString) select c).Distinct());

            if (musicAlbumResultsCollection.Count > 0)
                resultsListView.Items.Add("Album matches: " + (from r in musicAlbumResultsCollection select r.Tag.Album).Distinct().Count().ToString());
              

            // song results
            musicSongResultsCollection = new Collection<MediaFile>((from c in musicCollection where c.Tag.Title.ToLower().Contains(searchString) select c).Distinct());

            if (musicSongResultsCollection.Count > 0)
                resultsListView.Items.Add("Song matches: " + (from r in musicSongResultsCollection select r.Tag.Title).Distinct().Count().ToString());


            // genre results
            musicGenreResultsCollection = new Collection<MediaFile>((from c in musicCollection where c.Tag.Genre.ToLower().Contains(searchString) select c).Distinct());

            if (musicGenreResultsCollection.Count > 0)
                resultsListView.Items.Add("Genre matches: " + (from r in musicGenreResultsCollection select r.Tag.Genre).Distinct().Count().ToString());


            // video results
            videoResultsCollection = new Collection<MediaFile>((from c in videoCollection where c.Tag.Title.ToLower().Contains(searchString) select c).Distinct());

            if (videoResultsCollection.Count > 0)
                resultsListView.Items.Add("Video matches: " + (from r in videoResultsCollection select r.Tag.Title).Distinct().Count().ToString());


            // playlist results
            playlistResultsCollection = new Collection<Playlist>((from c in playlistCollection where c.Title.ToLower().Contains(searchString) select c).Distinct());

            if (playlistResultsCollection.Count > 0)
                resultsListView.Items.Add("Playlist matches: " + (from r in playlistResultsCollection select r.Title).Distinct().Count().ToString());

            if (resultsListView.Items.Count > 0)
                resultsListView.SelectedIndex = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void resultsListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (resultsListView.SelectedItem != null)
            {
                if (((string)resultsListView.SelectedItem).StartsWith("Artist m"))
                    detailedResultsListView.ItemsSource = musicArtistResultsCollection.Take(250);
                else if (((string)resultsListView.SelectedItem).StartsWith("Album m"))
                    detailedResultsListView.ItemsSource = musicAlbumResultsCollection.Take(250);
                else if (((string)resultsListView.SelectedItem).StartsWith("Song m"))
                    detailedResultsListView.ItemsSource = musicSongResultsCollection.Take(250);
                else if (((string)resultsListView.SelectedItem).StartsWith("Genre m"))
                    detailedResultsListView.ItemsSource = musicGenreResultsCollection.Take(250);
                else if (((string)resultsListView.SelectedItem).StartsWith("Video m"))
                    detailedResultsListView.ItemsSource = videoResultsCollection.Take(250);
                else if (((string)resultsListView.SelectedItem).StartsWith("Playlist m"))
                    detailedResultsListView.ItemsSource = playlistResultsCollection.Take(250);

                detailedResultsListView.Items.Refresh();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void selectAllMenuItem_OnClick(object sender, EventArgs e)
        {
            this.detailedResultsListView.SelectAll();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void invertSelectionMenuItem_OnClick(object sender, EventArgs e)
        {
            IEnumerator enum1 = this.detailedResultsListView.ItemsSource.GetEnumerator();
            ArrayList mediaToSelect = new ArrayList();

            while (enum1.MoveNext())
            {
                MediaFile media = (MediaFile)enum1.Current;

                if (!this.detailedResultsListView.SelectedItems.Contains(media))
                    mediaToSelect.Add(media);
            }

            this.detailedResultsListView.SelectedItems.Clear();

            foreach (MediaFile media in mediaToSelect)
                this.detailedResultsListView.SelectedItems.Add(media);

            this.detailedResultsListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearSelectionMenuItem_OnClick(object sender, EventArgs e)
        {
            this.detailedResultsListView.SelectedItems.Clear();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void randomizeMenuItem_OnClick(object sender, EventArgs e)
        {
            Collection<MediaFile> mediaToSave = new Collection<MediaFile>();

            foreach (MediaFile item in detailedResultsListView.Items)
                mediaToSave.AddItem(item);

            mediaToSave.RandomizeItems();
            detailedResultsListView.ItemsSource = mediaToSave;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void reverseListMenuItem_OnClick(object sender, EventArgs e)
        {
            Collection<MediaFile> mediaToSave = new Collection<MediaFile>();

            foreach (MediaFile item in detailedResultsListView.Items)
                mediaToSave.AddItem(item);

            mediaToSave.ReverseItems();
            detailedResultsListView.ItemsSource = mediaToSave;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sortByTitleMenuItem_OnClick(object sender, EventArgs e)
        {
            Collection<MediaFile> mediaToSave = new Collection<MediaFile>();

            foreach (MediaFile item in detailedResultsListView.Items)
                mediaToSave.AddItem(item);

            mediaToSave.Sort(new SortMediaByTitle());
            detailedResultsListView.ItemsSource = mediaToSave;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sortByPathMenuItem_OnClick(object sender, EventArgs e)
        {
            Collection<MediaFile> mediaToSave = new Collection<MediaFile>();

            foreach (MediaFile item in detailedResultsListView.Items)
                mediaToSave.AddItem(item);

            mediaToSave.Sort(new SortMediaByPath());
            detailedResultsListView.ItemsSource = mediaToSave;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void detailedResultsListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (detailedResultsListView.SelectedItem == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.AddItem(detailedResultsListView.SelectedItem as MediaFile);

            MediaFile media = (MediaFile)detailedResultsListView.SelectedItem;

            AMPLibrary.Media.MediaPlayer.GetMediaPlayer().Play(ref media);
        }

#region DetailedSearchResults

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                    playlistMedia.AddItem(selectedMedia);

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

#endregion

#region DetailedSearchResults

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNowPlayingDetailedResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();

            foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
            {
                if (selectedMedia == null)
                    return;

                nowPlayingCollection.AddItem(selectedMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void replaceNowPlayingDetailedResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            NowPlayingCollection nowPlayingCollection = NowPlayingCollection.GetNowPlayingCollection();
            nowPlayingCollection.ClearItems();

            foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                nowPlayingCollection.AddItem(selectedMedia);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToPlaylistDetailedResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            if (this.detailedResultsListView.SelectedItems.Count < 1)
                return;

            PlaylistSelectionWindow psw = new PlaylistSelectionWindow();

            if (psw.ShowDialog().Value == true)
            {
                Playlist playlist = psw.SelectedPlaylist;

                Collection<MediaFile> playlistMedia = new Collection<MediaFile>();

                foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                {
                    if (selectedMedia == null)
                        return;

                    playlistMedia.AddItem(selectedMedia);
                }

                Playlist.AddItems(playlist.Path, playlistMedia);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addToNewPlaylistDetailedResult_OnClick(object sender, RoutedEventArgs e)
        {
            if (this.detailedResultsListView.SelectedItems == null)
                return;

            if (this.detailedResultsListView.SelectedItems.Count < 1)
                return;

            Collection<MediaFile> newPlaylistMedia = new Collection<MediaFile>();

            foreach (MediaFile selectedMedia in this.detailedResultsListView.SelectedItems)
                newPlaylistMedia.AddItem(selectedMedia);

            NewPlaylistWindow npw = new NewPlaylistWindow(newPlaylistMedia);

            if (npw.ShowDialog().Value == true)
            {
                PlaylistCollection playlistCollection = PlaylistCollection.GetPlaylistCollection();
                playlistCollection.AddItem(new Playlist() { Path = npw.PlaylistPath, Title = System.IO.Path.GetFileName(npw.PlaylistPath) });
            }
        }

#endregion

    }
}
